package com.kede.app.service.impl;

import com.kede.app.domain.ZhiAction;
import com.kede.utils.ValidationUtil;
import com.kede.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.kede.app.repository.ZhiActionRepository;
import com.kede.app.service.ZhiActionService;
import com.kede.app.service.dto.ZhiActionDto;
import com.kede.app.service.dto.ZhiActionQueryCriteria;
import com.kede.app.service.mapstruct.ZhiActionMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.kede.utils.PageUtil;
import com.kede.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @description 服务实现
* @author Pr.li
* @date 2023-07-02
**/
@Service
@RequiredArgsConstructor
public class ZhiActionServiceImpl implements ZhiActionService {

    private final ZhiActionRepository zhiActionRepository;
    private final ZhiActionMapper zhiActionMapper;

    @Override
    public Map<String,Object> queryAll(ZhiActionQueryCriteria criteria, Pageable pageable){
        Page<ZhiAction> page = zhiActionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(zhiActionMapper::toDto));
    }

    @Override
    public List<ZhiActionDto> queryAll(ZhiActionQueryCriteria criteria){
        return zhiActionMapper.toDto(zhiActionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ZhiActionDto findById(Integer id) {
        ZhiAction zhiAction = zhiActionRepository.findById(id).orElseGet(ZhiAction::new);
        ValidationUtil.isNull(zhiAction.getId(),"ZhiAction","id",id);
        return zhiActionMapper.toDto(zhiAction);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZhiActionDto create(ZhiAction resources) {
        return zhiActionMapper.toDto(zhiActionRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ZhiAction resources) {
        ZhiAction zhiAction = zhiActionRepository.findById(resources.getId()).orElseGet(ZhiAction::new);
        ValidationUtil.isNull( zhiAction.getId(),"ZhiAction","id",resources.getId());
        zhiAction.copy(resources);
        zhiActionRepository.save(zhiAction);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            zhiActionRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ZhiActionDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZhiActionDto zhiAction : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("传递参数名", zhiAction.getParam());
            map.put("传递参数值", zhiAction.getValue());
            map.put("置换分类的节点id", zhiAction.getNodeId());
            map.put("动作金额", zhiAction.getActionMoney());
            map.put(" createTime",  zhiAction.getCreateTime());
            map.put(" updateTime",  zhiAction.getUpdateTime());
            map.put(" createBy",  zhiAction.getCreateBy());
            map.put(" updateBy",  zhiAction.getUpdateBy());
            map.put("说明", zhiAction.getRemark());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}